home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
randbg
/
src
/
randbg.c
Wrap
C/C++ Source or Header
|
1993-07-08
|
6KB
|
283 lines
/************************************************************************/
/* */
/* RandBG.c for T-OS V2.1L20 */
/* */
/**************************************** copyright 1992 T.Nakatani *****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <sys\stat.h>
#include <dos.h>
#include <fcntl.h>
#ifdef LSI_C
#define _O_RDONLY O_RDONLY
#define _O_WRONLY O_WRONLY
#define _O_RDWR O_RDWR
#define COPYBUFSIZE 57344
#endif
#ifdef __HIGHC__
#define COPYBUFSIZE tifsize
#endif
char sclfile[80];
char target[80];
char drive;
unsigned long dskfree, tifsize;
#ifdef TIMER
unsigned long before, after;
#endif
void errexit()
{
printf("RANDBG:%sは更新されません。\n", target);
exit(1);
}
int copy(char *name1, char *name2)
{
FILE *fr,*fw;
unsigned dt, tm;
char *buf;
long remain = tifsize, done, i;
#ifdef TIMER
#ifdef LSI_C
struct dostime_t cl;
_dos_gettime(&cl);
before = (unsigned long)cl.hsecond + (unsigned long)cl.second*100L +
(unsigned long)cl.minute*6000L + (unsigned long)cl.hour*360000L;
#endif
#ifdef __HIGHC__
before = clock();
#endif
/*
printf("COPY:%s to %s\n", name1, name2);
printf("TIF:%ld(%ld)\n",tifsize,remain);
*/
#endif
fr = fopen(name1, "rb");
if (fr == NULL) {
printf("RANDBG:%sがオープン出来ません。\n",name1);
errexit();
}
fw = fopen(name2, "wb");
if (fw == NULL) {
printf("RANDBG:%sがオープン出来ません。\n",name2);
fclose(fr);
errexit();
}
if ((buf = malloc(COPYBUFSIZE)) == NULL) {
#ifdef __HIGHC__
if ((buf = malloc(154112)) == NULL) {
clearerr(fr);
for (i = 0; i < remain; i++) {
fputc(fgetc(fr), fw);
}
}
#else
clearerr(fr);
for (i = 0; i < remain; i++) {
fputc(fgetc(fr), fw);
}
#endif
}
if (buf != NULL) {
while (remain > 0L) {
done = ((remain < COPYBUFSIZE) ? remain : COPYBUFSIZE);
fread(buf, 1, done, fr);
fwrite(buf, 1, done, fw);
remain -= done;
}
}
if ( _dos_getftime(fileno(fr), &dt, &tm) == 0)
_dos_setftime(fileno(fw), dt, tm);
fclose(fr);
fclose(fw);
#ifdef TIMER
#ifdef LSI_C
_dos_gettime(&cl);
after = (unsigned long)cl.hsecond + (unsigned long)cl.second*100L +
(unsigned long)cl.minute*6000L + (unsigned long)cl.hour*360000L;
#endif
#ifdef __HIGHC__
after = clock();
#endif
printf("実質コピー時間:%2ld.%02ld秒\n", (after-before)/100L, (after-before)%100L);
#endif
return (0);
}
void randbg()
{
FILE *fp;
int i, ret = 1;
int rnd;
unsigned long seed;
int maxc, times = 0;
int len;
char *buf, *now;
struct stat sbuf;
struct find_t fcb;
struct diskfree_t dinf;
char tiffile[80];
long clsize;
if (_dos_getdiskfree(drive - 0x40, &dinf) != 0) {
printf("RANDBG:%cドライブには出力出来ません\n", drive);
errexit();
}
else {
clsize = (long)dinf.sectors_per_cluster * (long)dinf.bytes_per_sector;
dskfree = (long)dinf.avail_clusters * clsize;
}
if (_dos_findfirst(target,
_A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY, &fcb) == 0) {
dskfree += ((((fcb.size - 1) / clsize) + 1L) * clsize);
}
if (stat(sclfile, &sbuf) != 0) {
printf("RANDBG:スケジュールファイルが見つかりません。\n");
errexit();
}
buf = malloc(sbuf.st_size + 1);
if (buf == NULL) {
printf("RANDBG:スケジュールファイルのサイズが大きすぎます。");
#ifdef LSI_C
printf("EXP版を使用してください。");
#endif
printf("\n");
errexit();
}
if ((fp = fopen(sclfile, "rt")) == NULL) {
printf("RANDBG:スケジュールファイルが見つかりません。\n");
errexit();
}
len = fread(buf, 1, sbuf.st_size, fp);
fclose(fp);
buf[len] = '\0';
now = buf;
maxc = 0;
while (now != NULL) { /* 行数を数えている */
maxc++;
now++;
now = strchr(now, '\n');
}
seed = clock();
srand(seed);
while (ret != 0) {
rnd = rand() % maxc;
now = buf;
if (rnd > 0) {
for (i = 0; i < rnd; i++) {
now = strchr(now, '\n') + 1;
}
}
sscanf(now, "%[^\n\t ]", tiffile);
if (*now == '\0') continue;
ret = _dos_findfirst(tiffile,
_A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY, &fcb);
tifsize = fcb.size;
if ((ret == 0) && (dskfree < tifsize)) {
printf("RANDBG:%cドライブの空き容量が小さいか、<%s>のサイズが大きすぎます。他のファイルを探します。\n", drive, tiffile);
ret = -1;
}
times++;
if (times > maxc) {
printf("RANDBG:使用出来ないファイルまたはコメント行が多すぎます。\n");
errexit();
}
/*
printf("seed = %ld / rnd = %d / maxc = %d\n", seed, rnd, maxc);
*/
}
free(buf);
copy(tiffile, target);
}
void swchk(int argc, char **argv)
{
int i;
unsigned int driveno;
if (argc < 4) {
printf("Background Screen Random Exchenger for T-OS V2.1L20 copyright 1992 T.Nakatani\n");
}
sclfile[0] = '\0';
drive =0;
*target = '\0';
for (i = 1; i < ((argc < 3) ? argc: 3); i++) {
switch(argv[i][0]) {
case '@':
strcpy(sclfile,&argv[i][1]);
break;
case '/':
if ((argv[i][2] == '\0') || *(strchr(argv[i], '\0') - 1) == ':') {
drive = toupper(argv[i][1]);
}
else {
strcpy(target, &(argv[i][1]));
}
break;
default:
printf("USAGE: randbg @d:\\exe\\randbg.scl /d:\n ^^^^^^^^^^^^^^^^^ ^^\n .SCLfile(fullpath) SYSTEM-Drive\n");
strcpy(target, "TMENU.TIF");
errexit();
}
}
if (sclfile[0] == '\0') {
strcpy(sclfile,argv[0]);
strcpy(strrchr(sclfile, '.'), ".scl");
}
if (drive == 0) {
_dos_getdrive(&driveno);
drive = 0x40 + driveno;
if (*target == '\0') {
sprintf(target, "%c:\\TMENU.TIF", drive);
}
}
}
int main(int argc,char **argv)
{
swchk(argc,argv);
randbg();
return (0);
}